package com.nut.course.api.partner;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.course.common.Holder;
import com.nut.course.common.Result;
import com.nut.course.entity.PartnerTeacher;
import com.nut.course.interceptor.UserJwt;
import com.nut.course.service.PartnerTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/partnerTeacher")
public class PartnerTeacherController {

    @Autowired
    private PartnerTeacherService partnerTeacherService;

    //教师分页列表
    @GetMapping("list")
    public Result<IPage<PartnerTeacher>> list(
            @RequestParam String name,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        UserJwt userJwt = Holder.USER_JWT_THREAD_LOCAL.get();
        Integer partnerId = Integer.valueOf(userJwt.getUserId());

        return partnerTeacherService.list(partnerId, name, pageNo, pageSize);
    }

    @GetMapping(value = "/{id}")
    public Result<PartnerTeacher> detail(@PathVariable Integer id) {
        return partnerTeacherService.getById(id);
    }

    @PostMapping
    public Result add(@RequestBody PartnerTeacher partnerTeacher) {

        UserJwt userJwt = Holder.USER_JWT_THREAD_LOCAL.get();
        Integer partnerId = Integer.valueOf(userJwt.getUserId());
        partnerTeacher.setPartnerId(partnerId);

        return partnerTeacherService.add(partnerTeacher);
    }

    @PutMapping
    public Result update(@RequestBody PartnerTeacher partnerTeacher) {
        return partnerTeacherService.updateById(partnerTeacher);
    }

    @DeleteMapping
    public Result delete(@RequestBody Set<Integer> ids) {
        return partnerTeacherService.deleteByIds(ids);
    }


}
